home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 3.0a2 / Libraries / UDeskScrapView.cp < prev    next >
Encoding:
Text File  |  1991-05-01  |  7.9 KB  |  310 lines  |  [TEXT/MPS ]

  1. // UDeskScrapView.cp
  2. // Copyright © 1985-1991 Apple Computer, Inc.  All rights reserved.
  3.  
  4. #ifndef __UDESKSCRAPVIEW__
  5. #include <UDeskScrapView.h>
  6. #endif
  7.  
  8. #ifndef __STDIO__
  9. #include <Stdio.h>
  10. #endif
  11.  
  12. #ifndef __UGEOMETRY__
  13. #include <UGeometry.h>
  14. #endif
  15.  
  16. #ifndef __ERRORS__
  17. #include <Errors.h>
  18. #endif
  19.  
  20. #ifndef __UCLIPBOARDMGR__
  21. #include <UClipboardMgr.h>
  22. #endif
  23.  
  24. #ifndef __UMACAPPUTILITIES__
  25. #include <UMacAppUtilities.h>
  26. #endif
  27.  
  28. #ifndef __UMEMORY__
  29. #include <UMemory.h>
  30. #endif
  31.  
  32. #ifndef __UMACAPPGLOBALS__
  33. #include <UMacAppGlobals.h>
  34. #endif
  35.  
  36.  
  37. Boolean gVarClipPicSize;                        // declared in interface
  38.  
  39. //--------------------------------------------------------------------------------------------------
  40. #pragma segment MAInit
  41. pascal void TDeskScrapView::Initialize(void)    // override 
  42. {
  43.     inherited::Initialize();
  44.     
  45.     fDataHandle = NULL;
  46.     fHavePicture = FALSE;
  47.     fHaveText = FALSE;
  48.     fScrapCount = 0;
  49. }
  50.  
  51. //--------------------------------------------------------------------------------------------------
  52. #pragma segment MAInit
  53.  
  54. pascal void TDeskScrapView::IDeskScrapView(void)
  55. {
  56.     VPoint itsSize(200, 200);
  57.     PScrapStuff aPtr;
  58.  
  59.     // Can't know the extent required until we actually read the data in, which we won't bother
  60.     // to do unless forced to by receiving an Update event
  61.  
  62.     this->IView(NULL, NULL, gZeroVPt, itsSize, sizeVariable, sizeVariable);
  63.  
  64.     aPtr = InfoScrap();
  65.     fScrapCount = aPtr->scrapCount - 1;
  66. }
  67.  
  68. //--------------------------------------------------------------------------------------------------
  69. #pragma segment MAInit
  70.  
  71. pascal void TDeskScrapView::IRes(TDocument* itsDocument,
  72.                                  TView* itsSuperView,
  73.                                  Ptr& itsParams)// override 
  74. {
  75.     PScrapStuff aPtr;
  76.  
  77.     inherited::IRes(itsDocument, itsSuperView, itsParams);
  78.  
  79.     aPtr = InfoScrap();
  80.     fScrapCount = (aPtr->scrapCount) - 1;
  81. }
  82.  
  83. //--------------------------------------------------------------------------------------------------
  84. #pragma segment MAClipboard
  85.  
  86. pascal void TDeskScrapView::Free(void)            // override 
  87. {
  88.     // Never want to free gClipboardMgr->fClipOrphanage.  What if some creates another view of this type? 
  89. }
  90.  
  91. //--------------------------------------------------------------------------------------------------
  92. #pragma segment MAClipboard
  93.  
  94. pascal void TDeskScrapView::CalcMinSize(VPoint& minSize)// override 
  95. {
  96.     VRect aVRect;
  97.     Rect aRect;
  98.     TEHandle aTEHandle;
  99.     Handle oldHText;
  100.  
  101.     minSize = fSize;
  102.     if (fDataHandle)
  103.         if (fHaveText)
  104.         {
  105.             // TEXT 
  106.             if (this->Focus())
  107.             {
  108.                 SetPortTextStyle(gSystemStyle);
  109.                 fSuperView->GetExtent(aVRect);
  110.                 aTEHandle = TENew(aVRect, aVRect);
  111.                 FailNIL((Ptr)aTEHandle);
  112.  
  113.                 oldHText = (*aTEHandle)->hText;    // We'll need this for TEDispose 
  114.                 (*aTEHandle)->hText = fDataHandle;
  115.  
  116.                 TECalText(aTEHandle);
  117.  
  118.                 minSize.h = (short) aVRect.Length(hSel);
  119.                 minSize.v = Min(IntMultiply((*aTEHandle)->nLines, (*aTEHandle)->lineHeight), kMaxCoord);
  120.                 (*aTEHandle)->hText = oldHText;    // So TEDispose doesn't dispose our handle 
  121.  
  122.                 TEDispose(aTEHandle);
  123.             }
  124.         }
  125.         else
  126.         {
  127. #if qDebugMsg
  128.             if (gIntenseDebugging)
  129.             {
  130.                 fprintf(stderr, "Picture Frame:");
  131.                 fprintf(stderr, "%s\n", (char *) ((**(PicHandle)fDataHandle).picFrame));
  132.             }
  133. #endif
  134.             for (VHSelect vhs = vSel; vhs <= hSel; ++vhs)
  135.                 if (!gVarClipPicSize)
  136.                 {
  137.                     aRect = (**((PicHandle)fDataHandle)).picFrame;// WITH PicHandle(fDataHandle)^^.picFrame DO
  138.                     minSize[vhs] = aRect[botRight][vhs] - aRect[topLeft][vhs];
  139.                 }
  140.         }
  141. }
  142.  
  143. //--------------------------------------------------------------------------------------------------
  144. #pragma segment MAClipboard
  145.  
  146. pascal Boolean LookForScrapType(ResType theResType, ResType& resTypeFound)
  147. {
  148.     long err;
  149.     long offset;
  150.  
  151.     err = GetScrap(NULL, theResType, offset);
  152.     if (err > 0)
  153.         resTypeFound = theResType;
  154. #if qDebugMsg
  155.     if ((err != noTypeErr) && (err < 0))
  156.         fprintf(stderr, "LookForScrapType: err = %d\n", err);
  157. #endif
  158.  
  159.     return (err > 0);
  160. }
  161.  
  162. //--------------------------------------------------------------------------------------------------
  163. #pragma segment MAClipboard
  164.  
  165. pascal void TDeskScrapView::CheckScrapContents(void)
  166. {
  167.     long         length;
  168.     long         offset;
  169.     Handle         aHandle;
  170.     ResType     resTypeFound;
  171.     Boolean     savedPerm;
  172.     FailInfo     fi;
  173.  
  174.     VOLATILE(aHandle);
  175.  
  176.     if (fScrapCount != gClipboardMgr->fNewScrapStuff.scrapCount)
  177.     {
  178.         // Make sure we get rid of the old scrap handle if necessary 
  179.         fDataHandle = DisposeIfHandle(fDataHandle);
  180.  
  181.         fHavePicture = LookForScrapType('PICT', resTypeFound);
  182.         fHaveText = LookForScrapType('TEXT', resTypeFound);
  183.  
  184.         if (fHavePicture || fHaveText)
  185.         {
  186.             aHandle = NULL;
  187.             aHandle = NewPermHandle(0);
  188.  
  189.             if (fi.Try())
  190.             {
  191.                 savedPerm = PermAllocation(TRUE);
  192.                 length = GetScrap(aHandle, resTypeFound, offset);
  193.                 savedPerm = PermAllocation(savedPerm);
  194.  
  195.                 if (length < 0)                    // Only results < 0 are really errors 
  196.                     FailOSErr((OSErr)length);
  197.                 fDataHandle = aHandle;
  198.                 this->AdjustSize();
  199.                 this->Focus();                    // Make sure we're still focused 
  200.                 fi.Success();
  201.             }
  202.             else    // Recover
  203.             {
  204.                 aHandle = DisposeIfHandle(aHandle);
  205.                 fDataHandle = NULL;
  206.                 fi.ReSignal();
  207.             }
  208.         }
  209.     }
  210. }
  211.  
  212. //--------------------------------------------------------------------------------------------------
  213. #pragma segment MAClipboard
  214.  
  215. pascal void TDeskScrapView::Draw(const VRect& area)
  216. {
  217.     Rect aRect;
  218.     SignedByte savedState;
  219.  
  220. #if qDebug
  221.     this->AssumeFocused();
  222. #endif
  223.  
  224.     // Has scrap been updated since we last drew? 
  225.     this->CheckScrapContents();
  226.  
  227.     if (fHaveText)
  228.     {
  229.         SetPortTextStyle(gApplicationStyle);
  230.         this->GetQDExtent(aRect);
  231.  
  232.         // Does TextEdit handle failure in TextBox??? 
  233.         savedState = LockHandleHigh(fDataHandle);// Because MATextBox may move memory 
  234.         MATextBox(*fDataHandle, GetHandleSize(fDataHandle), aRect, teFlushDefault, kAutoWrap, NULL, kNoEraseFirst, kNoSpaceForCaret);
  235.         HSetState(fDataHandle, savedState);
  236.     }
  237.     else if (fHavePicture)
  238.     {
  239.         if (gVarClipPicSize)
  240.             this->GetQDExtent(aRect);
  241.         else
  242.         {
  243.             aRect = (**((PicHandle)fDataHandle)).picFrame;
  244.             for (VHSelect vhs = vSel; vhs <= hSel; ++vhs)
  245.             {
  246.                 aRect[botRight][vhs] = aRect[botRight][vhs] - aRect[topLeft][vhs];
  247.                 aRect[topLeft][vhs] = 0;
  248.             }
  249.         }
  250.         DrawPicture((PicHandle)fDataHandle, aRect);
  251.     }
  252.  
  253.     if (fScrapCount != (gClipboardMgr->fNewScrapStuff.scrapCount))
  254.     {
  255.         this->ValidateRect(aRect);                // avoid flicker upon clipboard installation
  256.                                                 // -- but could this cause failure to update
  257.                                                 // frame border/appendages sometimes, if that
  258.                                                 // code ever checked for visibility before drawing ???
  259.         fScrapCount = (gClipboardMgr->fNewScrapStuff.scrapCount);
  260.         gClipboardMgr->fClipWrittenToDeskScrap = TRUE;// Don't need to write it back out. 
  261.     }
  262.  
  263.     inherited::Draw(area);
  264. }
  265.  
  266. //--------------------------------------------------------------------------------------------------
  267. #pragma segment MAFields
  268.  
  269. pascal void TDeskScrapView::Fields(TObject* obj)
  270. {
  271.     obj->DoToField("TDeskScrapView", NULL, bClass);
  272.     obj->DoToField("fHavePicture", &fHavePicture, bBoolean);
  273.     obj->DoToField("fHaveText",  &fHaveText, bBoolean);
  274.     obj->DoToField("fScrapCount", &fScrapCount, bInteger);
  275.     obj->DoToField("fDataHandle", &fDataHandle, bHandle);
  276.  
  277.     inherited::Fields(obj);
  278. }
  279.  
  280. //--------------------------------------------------------------------------------------------------
  281. #pragma segment MAInspector
  282.  
  283. pascal void TDeskScrapView::GetInspectorName(Str255& inspectorName)// override 
  284. {
  285.     if (this == gClipboardMgr->fClipView)
  286.         inspectorName = "gClipboardMgr->fClipView";
  287.     else if (this == gClipboardMgr->fClipOrphanage)
  288.         inspectorName = "gClipboardMgr->fClipOrphanage";
  289. }
  290.  
  291. //--------------------------------------------------------------------------------------------------
  292. #pragma segment MAClipboard
  293.  
  294. pascal void TDeskScrapView::SuperViewChangedSize(const VPoint& ,
  295.                                                  Boolean)// override 
  296. {
  297.     this->AdjustSize();
  298.     this->ForceRedraw();
  299. }
  300.  
  301. //--------------------------------------------------------------------------------------------------
  302. #pragma segment MAClipboard
  303.  
  304. pascal void TDeskScrapView::WriteToDeskScrap(void)// override 
  305. {
  306.     // This view represents data that's already written to the desk scrap 
  307. }
  308.  
  309.  
  310.